﻿using System;
using System.Collections.Generic;

namespace Microsoft.Xna.Framework.Graphics
{
	/// <summary>
	/// Manipulates a collection of EffectParameter objects.
	/// </summary>
	public sealed class EffectParameterCollection : IEnumerable<EffectParameter>
	{
		#region Fields
		private Effect _parent;
		private List<EffectParameter> pParameter;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the number of EffectParameter objects in this EffectParameterCollection.
		/// </summary>
		public int Count
		{
			get { return pParameter.Count; }
		}

		/// <summary>
		/// Gets a specific EffectParameter object by using an index value.
		/// </summary>
		/// <param name="index">Index of the EffectParameter to get.</param>
		public EffectParameter this[int index]
		{
			get
			{
				if ((index >= 0) && (index < this.pParameter.Count))
				{
					return this.pParameter[index];
				}
				return null;
			}
		}

		/// <summary>
		/// Gets a specific EffectParameter by name.
		/// </summary>
		/// <param name="name">The name of the EffectParameter to retrieve.</param>
		public EffectParameter this[string name]
		{
			get
			{
				List<EffectParameter>.Enumerator enumerator = this.pParameter.GetEnumerator();
				if (enumerator.MoveNext())
				{
					do
					{
						EffectParameter current = enumerator.Current;
						if (current._name == name)
						{
							return current;
						}
					}
					while (enumerator.MoveNext());
				}
				return null;
			}
		}
		#endregion

		#region Constructor
		internal EffectParameterCollection(Effect effect, int count, bool arrayElements)
		{
			this._parent = effect;
			this.pParameter = new List<EffectParameter>(count);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Gets an enumerator that can iterate through EffectParameterCollection.
		/// </summary>
		public List<EffectParameter>.Enumerator GetEnumerator()
		{
			return this.pParameter.GetEnumerator();
		}

		/// <summary>
		/// Gets an effect parameter from its semantic usage.
		/// </summary>
		/// <param name="semantic">The semantic meaning, or usage, of the parameter.</param>
		public EffectParameter GetParameterBySemantic(string semantic)
		{
			List<EffectParameter>.Enumerator enumerator = this.pParameter.GetEnumerator();
			if (enumerator.MoveNext())
			{
				do
				{
					EffectParameter current = enumerator.Current;
					if (string.Compare(current._semantic, semantic, StringComparison.OrdinalIgnoreCase) == 0)
					{
						return current;
					}
				}
				while (enumerator.MoveNext());
			}
			return null;
		}
		#endregion

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.pParameter.GetEnumerator();
		}

		IEnumerator<EffectParameter> IEnumerable<EffectParameter>.GetEnumerator()
		{
			return this.pParameter.GetEnumerator();
		}
	}
}
